package com.tbit.uqbike.client.controller;


import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.dto.LowCarbonDTO;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.service.LowCarbonService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/lowCarbon")
public class LowCarbonController {
    @Autowired
    private LowCarbonService lowCarbonService;
    @Autowired
    private TokenService tokenService;

    /**
     * @description: 获取订单的低碳骑行信息
     * @author: mo.shanyong
     * @date: 2025/5/17 17:08
     * @param: orderNO
     * @return: com.tbit.common.entity.view.Result<com.tbit.uqbike.client.dto.LowCarbonDTO>
     */
    @GetMapping("/getLowCarbonInfoByOrderNO")
    public void getLowCarbonInfoByOrderNO(@NotNull String orderNO, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            LowCarbonDTO lowCarbonDTO = lowCarbonService.getLowCarbonInfoByOrderNO(orderNO);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, lowCarbonDTO);
        }catch (BaseException exception){
            ResponseCode.printException(map, exception, request);
        }
        OutputUtil.renderText(request, response, map);
    }
    /**
     * @description: 根据时间区间获取用户的低碳骑行数据
     * @author: mo.shanyong
     * @date: 2025/5/17 17:10
     * @param: token
     * @param: startTime
     * @param: endTime
     * @return: com.tbit.common.entity.view.Result<com.tbit.uqbike.client.dto.LowCarbonDTO>
     */
    @GetMapping("/getLowCarbonInfoByTime")
    public void getLowCarbonInfoByTime(@NotNull String token,@NotNull String startTime, @NotNull String endTime,
                                       HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            LowCarbonDTO lowCarbonDTO = lowCarbonService.getLowCarbonInfoByTime(tokenService.getUser(token), startTime, endTime);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, lowCarbonDTO);
        }catch (BaseException exception){
            ResponseCode.printException(map, exception, request);
        }
        OutputUtil.renderText(request, response, map);
    }

}
